package com.ruoyi.test;

import com.ruoyi.common.utils.StringUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProjectCodeAnalyzer  {


   static List<Key> listA = new ArrayList<>();

    public static void main(String[] args) {
//        String projectPath = "D:\\SH\\SH7\\lup-event-dimension"; // 替换为你的Java项目路径
        String projectPath = "D:\\liangma_new\\lup_datacenter_business"; // 替换为你的Java项目路径
//        String projectPath = "D:\\liangma_new\\lup-event-workbill"; // 替换为你的Java项目路径
        searchInDirectory(new File(projectPath));
        writeListToFile(listA, "D:\\Sh4\\ruoyi-plus-tdengine\\ruoyi-admin\\src\\test\\java\\com\\ruoyi\\test\\out.txt");

    }

    public static void writeListToFile(List<Key> list, String filePath) {
        try (FileWriter writer = new FileWriter(filePath)) {
            for (Key line : list) {
//                String a = line.getKey()+"="+line.getContent()
//                      +"                    {}"+line.getOrgContent()+"{}" + line.getFilePath();


                String a = line.getKey()+"="+line.getContent() ;

                writer.write(a + System.lineSeparator());
            }
            System.out.println("List内容已写入到文件: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void searchInDirectory(File directory) {
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        searchInDirectory(file);
                    } else if (file.getName().endsWith(".java")) {
                        searchInFile(file);
                    }
                }
            }
        }
    }

    private static void searchInFile(File file) {
        try {
            List<String> lines = Files.readAllLines(file.toPath());
            Pattern pattern = Pattern.compile("(throw\\s+[^\\s;]+\\s*|\\bResult\\.fail\\b|\\bResult\\.success\\b|\\bAssert\\.notNull\\b|\\bAssert\\.hasText\\b|\\bAssert\\.isTrue\\b|\\bAssert\\.(?!notNull|hasText|isTrue)\\w+\\b|\\benum\\s+\\w+\\b)");
            int lineNumber = 0;
            for (String line : lines) {
                lineNumber++;
                Matcher matcher = pattern.matcher(line);
                while (matcher.find()) {
                    if (line.trim() .contains("e.getMessage()")){

                    }else {
                        //D:\liangma_new\lup_datacenter_business\lup-cms
                        if ( file.getPath().contains("lup-workflow")
//                        || file.getPath().contains("lup-datacenter-server")
//                        || file.getPath().contains("lup-datacenter-common")
//                        || file.getPath().contains("lup-datacenter-websocket")
//                        || file.getPath().contains("lup-datacenter-api")
//                        || file.getPath().contains("lup-datacenter-mqtt")
//                        || file.getPath().contains("lup-mqtt")
                        ){
                            Key ee = new Key();
                            ee.setFilePath(file.getPath());
                            ee.setOrgContent(line.trim());
                            ee.setKey(deal(file.getPath() )+".");
                            ee.setContent( extractTextInQuotes2(line.trim()));
                            listA.add(ee);
                        }

                    }

                    System.out.println("文件: " + file.getPath() + "||行号: " + lineNumber + "||内容: " + line.trim() +"||key="+"||");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String deal( String path  ){
        String s = null;
        if (path.contains("lup-workflow")){
            s = "workflow";
        }
        if (path.contains("lup-datacenter-service")){
            s = "datacenter";
        }
        if (path.contains("lup-datacenter-server")){
            s = "datacenter";
        }
        if (path.contains("lup-datacenter-common")){
            s = "datacenter";
        }
        if (path.contains("lup-datacenter-websocket")){
            s = "datacenter";
        }
        if (path.contains("lup-datacenter-api")){
            s = "datacenter";
        }
        if (path.contains("lup-datacenter-mqtt")){
            s = "datacenter";
        }
        if (path.contains("lup-event-dimension")){
            s = "eventDimension";
        }
        if (path.contains("lup-mqtt")){
            s = "datacenter";
        }
        if (path.contains("lup-file")){
            s = "lupFile";
        }
        if (path.contains("lup-cms")){
            s = "lupCms";
        }
        if (path.contains("lup-config")){
            s = "lupConfig";
        }
        if (path.contains("lup-event-workbill")){
            s = "lupEventWorkbill";
        }
        if (path.contains("lup_event_stream")){
            s = "lupEventStream";
        }

        String[] parts = path.split("\\\\");
        String part = parts[parts.length - 1];
        String s1 = part.split("\\.")[0];
//        s1 = s1.replace("ServiceImpl","")
//            .replace("Controller","");
        s= s+"."+ s1.toLowerCase();
        return s;
    }
    public static String extractTextInQuotes2(String input) {
        String s = extractTextInQuotes(input);
        if (StringUtils.isBlank(s)){
            Pattern pattern = Pattern.compile("\\[([^\\[\\]]*)\\]");
            Matcher matcher = pattern.matcher(input);
            while (matcher.find()) {
                String contentInsideBrackets = matcher.group(1);
                // 对获取到的内容进行处理
                return  contentInsideBrackets;
            }
            return null;
        }else {
            return s;
        }
    }

    public static String extractTextInQuotes(String input) {
        Pattern pattern = Pattern.compile("\"([^\"]*)\"");
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            return matcher.group(1); // 返回双引号内的内容
        }
        return ""; // 如果没有找到匹配，则返回空字符串
    }

    @Test
    public  void  aa(){
        String input = "Assert.isTrue(id !=null, \"id没传\");";
        String textInQuotes = extractTextInQuotes(input);
        System.out.println(textInQuotes); // 输出：示例
    }
}
